package com.leetcode;

import com.leetcode.common.ListNode;

/**
 * 203. 移除链表元素
 * 迭代(不使用虚拟头节点)
 *
 * @author fy
 * @date 2022/4/21 19:24
 */
public class Solution203_3 {

    public ListNode removeElements(ListNode head, int val) {
        while (head != null && head.val == val) {
            head = head.next;
        }
        if (head == null) {
            return null;
        }
        // 命名prev更合理
        ListNode prev = head;
        while (prev.next != null) {
            if (prev.next.val == val) {
                prev.next = prev.next.next;
            } else {
                prev = prev.next;
            }
        }
        return head;
    }

}
